﻿using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
using TL_Cloud.Core.Entities;

namespace TL_Cloud.Core.LinkURL
{
    /// <summary>
    /// 连接URL创建者
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class LinkUrlCreater<T>
        where T : QueryParameters
    {
        private readonly IUrlHelper _urlHelper;
        public LinkUrlCreater(IUrlHelper urlHelper)
        {
            this._urlHelper = urlHelper;
        }
        /// <summary>
        /// 创建Get请求的url当中
        /// </summary>
        /// <param name="routeTemplateName">获取所有的数据的路由模板名称</param>
        /// <param name="parameters"></param>
        /// <param name="uriType"></param>
        /// <returns></returns>
        public string CreateGetUrl(string routeTemplateName, T parameters, PaginationResourceUriType uriType)
        {
            switch (uriType)
            {
                case PaginationResourceUriType.PreviousPage:
                    var previousParameters = new
                    {
                        pageIndex = parameters.PageIndex - 1,
                        pageSize = parameters.PageSize,
                        orderBy = parameters.OrderBy,
                        fields = parameters.Fields,
                        //title = parameters.Title
                    };
                    return _urlHelper.Link(routeTemplateName, previousParameters);
                case PaginationResourceUriType.NextPage:
                    var nextParameters = new
                    {
                        pageIndex = parameters.PageIndex + 1,
                        pageSize = parameters.PageSize,
                        orderBy = parameters.OrderBy,
                        fields = parameters.Fields,
                        //title = parameters.Title
                    };
                    return _urlHelper.Link(routeTemplateName, nextParameters);
                default:
                    var currentParameters = new
                    {
                        pageIndex = parameters.PageIndex,
                        pageSize = parameters.PageSize,
                        orderBy = parameters.OrderBy,
                        fields = parameters.Fields,
                        // title = parameters.Title
                    };
                    return _urlHelper.Link(routeTemplateName, currentParameters);
            }
        }

        /// <summary>
        /// 创建请求为Post请求
        /// </summary>
        /// <param name="getRouteTemplate">获取单个实体的连接,可以为空</param>
        /// <param name="deleteRouteTemplate">删除路由模板的名称 ,可以为空</param>
        /// <param name="id">单个实体的编号</param>
        /// <returns></returns>
        public IEnumerable<LinkResource> CreateLinksForPost(string getRouteTemplate, string deleteRouteTemplate, string id, string fields = null)
        {
            var links = new List<LinkResource>();

            if (!string.IsNullOrEmpty(getRouteTemplate))
            {
                if (string.IsNullOrWhiteSpace(fields))
                {
                    links.Add(
                        new LinkResource(
                            _urlHelper.Link(getRouteTemplate, new { id }), "self", "GET"));
                }
                else
                {
                    links.Add(
                        new LinkResource(
                            _urlHelper.Link(getRouteTemplate, new { id, fields }), "self", "GET"));
                }
            }
            if (!string.IsNullOrEmpty(deleteRouteTemplate))
            {
                links.Add(
                    new LinkResource(
                        _urlHelper.Link(deleteRouteTemplate, new { id }), "delete", "DELETE"));
            }
            return links;
        }

    }
}
